import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { DomSanitizer } from '@angular/platform-browser';
import { LocalStorage } from '@core/local.storage';
import { _HttpClient } from '@delon/theme';
import { environment } from '../../../../environments/environment';
import { forkJoin } from 'rxjs';

@Component({
    selector: 'app-detailmodal',
    templateUrl: './detailmodal.component.html',
    styleUrls: ['./signalProblem.component.less']
})
export class DetailModalComponent implements OnInit {
    @Input() apiUrl: any;
    @Input() width: any;
    @Input() responseData: any;
    @Input() popTitle: any;
    @Output() public closeModal = new EventEmitter();
    @Output() public reloadData = new EventEmitter();

    Visible = false;
    operaData: any = {};
    objData: any = {};
    thisData: any = { 'count': 0, 'id': [] };
    problemData: any = {};
    processesData = [];
    handlesData = [];

    constructor(
        private msg: NzMessageService,
        private sanitizer: DomSanitizer,
        public ls: LocalStorage,
        private http: _HttpClient
    ) { }

    ngOnInit() {
        if (!this.width) {
            this.width = '500';
        }
        let xqStatus = false;
        let xqCount = 0;
        for (const item of this.responseData) {
            if (item.checked) {
                xqCount += 1;
                this.objData = item;
                this.thisData.count += 1;
                xqStatus = true;
            }
        }
        if (xqStatus && xqCount === 1) {
            this.getOperaApi((res) => {
                this.problemData = res.problem;
                this.processesData = res.processes;
                this.handlesData = res.handles;
                for (const item of this.processesData) {
                    item.infoHtml = this.sanitizer.bypassSecurityTrustHtml(item.info);
                }
                for (const item of this.handlesData) {
                    item.handleHtml = this.sanitizer.bypassSecurityTrustHtml(item.handleInfo);
                }
            });
        } else {
            this.Cancel();
            this.msg.error('请选择一条数据进行操作');
        }
    }
    getOperaApi(callback) {
        setTimeout(() => {
            this.Visible = true;
            const xqApi = [
                this.http.get(environment.project + '/safeProblem/detail/' + this.objData.id)
            ];
            forkJoin(xqApi).subscribe((results: any) => {
                if (!results[0].code) {
                    if (callback) {
                        callback(results[0].data[0]);
                    }
                }
            });
        }, 200);
    }
    Ok() {
        this.Cancel();
    }
    Cancel() {
        this.Visible = false;
        setTimeout(() => {
            this.closeModal.emit();
        }, 200);
    }
}